#ifndef _MUNIX_TWI_H_
#define	_MUNIX_TWI_H_
/*******************************************************************************
 **  Name: twi.h                                                              **
 **  Description:                                                             **
 **                                                                           **
 **  CDDL: Open Source Initiative (OSI) Approved License                      **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
#define _MUNIX_TWI_H_ID "$Id$"
// Last Modified $DateTime$ by $Author$
#include <util/twi.h>
#include "common.h"

#define DEFAULT_TWI_SPEED   400

class TWI
{
public:
    TWI(uint8 i = 0, BusMode m = BusMode::Master);

    // implemented pure-virtual functions
    bool    Connect();
    void    SendByte(uint8 remote, char byte);
    void    SendWord(uint8 remote, wchar_t word);
    char    RecvByte();
    wchar_t RecvWord();

    inline void     StartCondition() { TWCR = ((1<<TWINT)|(1<<TWSTA)|(1<<TWEN)); }
    inline bool     StartOK() { return (TW_STATUS == TW_START); }
    inline bool     AddrOK() { return (TW_STATUS == TW_MT_SLA_ACK); }
    inline bool     DataOK() { return (TW_STATUS == TW_MT_DATA_ACK); }
    inline void     StopCondition() { TWCR = ((1<<TWINT)|(1<<TWEN)|(1<<TWSTO)); }
    inline void     WaitFlag() { while( !(TWCR & (1<<TWINT)) ); }

    void    Prescaler(uint8);
    void    Bitrate(uint8 b) { BSET(TWBR,b); }

private:
    int     wireSpeed;      // kHz
    BusMode mode;
};

#endif	/* _MUNIX_TWI_H_ */

